//
//  IPNewPlanViewController.swift
//  IPlan
//
//  Created by robot on 3/24/16.
//  Copyright © 2016 codeFighter.com. All rights reserved.
//

import UIKit
import RealmSwift

class IPNewPlanViewController: UIViewController, UITextViewDelegate {

    fileprivate var inputTextView: UITextView!
    fileprivate var commitButton: UIButton!
    fileprivate var importantButton: UIButton!
    fileprivate var normalButton: UIButton!
    fileprivate var unImportantButton:UIButton!
    fileprivate var contentTitle: UILabel!
    fileprivate var planLevel: UILabel!
    fileprivate var levelSelectedTag:NSInteger!
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        view.backgroundColor = UIColor.init(netHex: 0xF1F1F1)
        title = "新建计划"
        // Do any additional setup after loading the view.
        levelSelectedTag = 1000
        addContentView()
        creatConstrains()
        
    }

    func clickedCommitButton() -> Void {
        
        let plan = IPPlaneModel()
        
        plan.date = Date.init().getCurrentDateString()
        plan.content = inputTextView.text
        plan.userid = "13121188667"
        plan.level = levelSelectedTag - 1000
        let realm = try! Realm()
        realm.beginWrite()
        realm.add(plan)
        try! realm.commitWrite()
        self.navigationController?.popViewController(animated: true)
        
    }
    
    func levelButtonClicked(button:UIButton) ->Void {
    
        button.isSelected = true
        levelSelectedTag = button.tag
        let levelButtons = [importantButton, normalButton, unImportantButton]
        for buttonItem in levelButtons {
            
            if buttonItem?.tag == button.tag {
                continue
                
            }else{
            
               buttonItem?.isSelected = false
            }
        }

    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        
        if text == "\n" {
            
            textView.resignFirstResponder()
            return true
        }
    
        return true
    }
        
    override func viewDidLayoutSubviews (){
        
        super.viewDidLayoutSubviews()
        inputTextView.setContentOffset(CGPoint.zero, animated: false)
        inputTextView.isScrollEnabled = false
    }
    
    func addContentView() {
    
        contentTitle = UILabel()
        view.addSubview(contentTitle)
        contentTitle.text = "计划内容:"
        contentTitle.textColor = UIColor.black
        contentTitle.font = UIFont.systemFont(ofSize: 18)
        
        inputTextView = UITextView()
        view.addSubview(inputTextView)
        inputTextView.textAlignment = NSTextAlignment.left
        inputTextView.setContentOffset(CGPoint.zero, animated: false)
        inputTextView.backgroundColor = UIColor.white
        inputTextView.returnKeyType = .done
        inputTextView.delegate = self
        
        planLevel = UILabel()
        planLevel.text = "计划等级:"
        planLevel.font = UIFont.systemFont(ofSize: 18)
        planLevel.textColor = UIColor.black
        view.addSubview(planLevel)
        
        importantButton = UIButton()
        importantButton.isSelected = true
        importantButton.tag = 1000
        configLevelButton(button: importantButton, title: "重要")
        view.addSubview(importantButton)

        normalButton = UIButton()
        configLevelButton(button: normalButton, title: "一般")
        normalButton.tag = 1001
        view.addSubview(normalButton)
        
        unImportantButton = UIButton()
        configLevelButton(button: unImportantButton, title: "不重要")
        unImportantButton.tag = 1002
        view.addSubview(unImportantButton)
        
        commitButton = UIButton()
        view.addSubview(commitButton)
        commitButton.backgroundColor = UIColor.init(netHex: 0xE72A57)
        commitButton.setTitle("保存", for:.normal)
        commitButton.addTarget(self, action: #selector(IPNewPlanViewController.clickedCommitButton), for: .touchUpInside)
        commitButton.layer.borderWidth = 0
        commitButton.layer.cornerRadius = 8
        commitButton.clipsToBounds = true
    
    }
    
    func creatConstrains() {
    
        contentTitle.snp.makeConstraints { (make) in
            
            make.left.equalTo(15)
            make.top.equalTo(69)
            make.height.equalTo(29)
        }
        
        inputTextView.snp.makeConstraints { (make) in
            
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.top.equalTo(contentTitle.snp.bottom).offset(14)
            make.height.equalTo(100)
            
        }
        
        planLevel.snp.makeConstraints { (make) in
            
            make.left.equalTo(15)
            make.top.equalTo(inputTextView.snp.bottom).offset(40)
            make.width.equalTo(87)
            
        }
        
        importantButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(planLevel.snp.right).offset(20)
            make.centerY.equalTo(planLevel.snp.centerY)
            
        }
        
        normalButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(importantButton.snp.right).offset(15)
            make.centerY.equalTo(planLevel.snp.centerY)

        }
        
        unImportantButton.snp.makeConstraints { (make) in
            
            make.left.equalTo(normalButton.snp.right).offset(20)
            make.centerY.equalTo(planLevel.snp.centerY)
            
        }
        
        commitButton.snp.makeConstraints { (make) in
            
            make.top.equalTo(planLevel.snp.bottom).offset(82)
            make.left.equalTo(45)
            make.right.equalTo(-45)
            make.height.equalTo(50)
        }
        
    }
    
    func configLevelButton(button:UIButton,title:String!){
     
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(.black, for: UIControlState.normal)
        button.setImage(UIImage.init(named: "new_level_normal"), for: .normal)
        button.setImage(UIImage.init(named: "new_level_selected"), for: .selected)
        button.addTarget(self, action: #selector(IPNewPlanViewController.levelButtonClicked), for: UIControlEvents.touchUpInside)
        
    }
}
